﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Text.RegularExpressions;
using Project_Vital.Dao;
using Project_Vital.StringResources;

namespace Project_Vital
{
    public partial class SupplierDetails : Form
    {
      
       public SupplierDetails()
         {
           InitializeComponent();

          //  this.Text = FormTitle.FormNewSup;

          //  btnviewprd.Visible = false;
           // btnPayments.Visible = false;
            //btnEditDetails.Visible = false;
            //btnViewPrevOrder.Visible = false;
            //btnCreateOrder.Visible = false;
            //btnViewInvoice.Visible = true;
        } 
    
        private void tbSupEmail_Validating(object sender, CancelEventArgs e)
        {
            System.Text.RegularExpressions.Regex rEMail = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z][\w\.-]{2,28}[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
            if (tbSupEmail.Text.Length > 0)
            {
                if (!rEMail.IsMatch(tbSupEmail.Text))
                {
                    MessageBox.Show("E-Mail expected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    tbSupEmail.SelectAll();
                    e.Cancel = true;
                }
            }

        }
        
        public static bool ValidateUkPostcode(string postcode)
        {
         return Regex.Match(postcode, "(^gir\\s?0aa$)|(^[a-z-[qvx]](\\d{1,2}|[a-hk-y]\\d{1,2}|\\d[a-hjks-uw]|[a-hk-y]\\d[abehmnprv-y])\\s?\\d[a-z-[cikmov]]{2}$)", RegexOptions.IgnoreCase).Success;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            

            /*Surname, DoB, Contact Person, Business Name,Address 1, City, Postcode, Phone, Open Time, 
            Close Time are mandatory fields.*/

            if (string.IsNullOrEmpty(tSupname.Text))
            {
                MessageBox.Show("You must enter a Supplier Name.");
                return;
            }
           /* else if (string.IsNullOrEmpty( tbSupContP1.Text))
            {
                MessageBox.Show("You must enter a Contact Persons Name.");
                return;
            }
            else if (string.IsNullOrEmpty(  tbSupContP2.Text))
            {
                MessageBox.Show("You must enter a Contact Persons Name.");
                return;
            } */
      
           /* else if (string.IsNullOrEmpty(tbSupadd1.Text))
            {
                MessageBox.Show("You must enter a Business Address 1");
                return;
            }
            else if (string.IsNullOrEmpty(tbSupCity.Text))
            {
                MessageBox.Show("You must enter a City.");
                return;
            }
            else if (string.IsNullOrEmpty(tbSupCounty.Text))
            {
                MessageBox.Show("You must enter a County.");
                return;
            }
            else if (string.IsNullOrEmpty(tbsupcountry.Text))
            {
                MessageBox.Show("You must enter a Country.");
                return;
            }
            else if (string.IsNullOrEmpty(tbPostCode.Text))
            {
                MessageBox.Show("You must enter a POstCode.");
                return;
            }
            else if (string.IsNullOrEmpty(tbsupContact1.Text))
            {
                MessageBox.Show("You must enter a Contact No.");
                return;
            }
            else if (string.IsNullOrEmpty(SupOpenTime.Text))
            {
                MessageBox.Show("You must enter a Opening time.");
                return;
            }
            else if (string.IsNullOrEmpty(SupCloseTime.Text))
            {
                MessageBox.Show("You must enter a Closing Time.");
                return;
            } */
            else
            {
                SupplierDao supplierDao = new SupplierDao();
                supplierDao.CreateSupplier(this.CreateNewSupplier());
            }

        }

        private Supplier CreateNewSupplier()
        {
            Supplier supplier = new Supplier();
        //  supplier._vsup_supplier_id = "4";
            supplier._vsup_supplier_name = tSupname.Text;
            supplier._vsup_supplier_addr1 = tbSupadd1.Text;
            supplier._vsup_supplier_addr2 = tbSupadd2.Text;
            supplier._vsup_supplier_city = tbSupCity.Text;
            supplier._vsup_supplier_county = tbSupCounty.Text;
            supplier._vsup_supplier_country = tbsupcountry.Text;
            supplier._vsup_supplier_contact1 = tbSupContP1.Text;
            supplier._vsup_supplier_contact2 = tbSupContP2.Text;
            
           // supplier._vsup_postcode = tbPostCode.Text;
            supplier._vsup_supplier_phone1 = tbsupContact1.Text;
            supplier._vsup_supplier_phone2 = tbSupContact2.Text;
            supplier._vsup_supplier_mobile = tbSupMob.Text;
            supplier._vsup_supplier_fax = tbSupFax.Text;
            supplier._vsup_supplier_email = tbSupEmail.Text;
            //supplier._vsup_opening_time = SupOpenTime.ToString;
          //  supplier._vsup_closing_time = SupCloseTime.Value;
           
            return supplier;
        }

        /*Phone number and Mobile Number (If applicable) should also 
        be validated against correct UK format.*/

       
        private void tbCustContact_Validating(object sender, CancelEventArgs e)
        {
            Regex regexObj =
                new Regex(@"^(?:\+?0[-. ]?)?\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$");

            if (regexObj.IsMatch(tbsupContact1.Text))
            {
                string formattedPhoneNumber =
                    regexObj.Replace(tbsupContact1.Text, "($1) $2-$3");
            }
            else
            {
                MessageBox.Show("Please enter Correct Number ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    tbSupEmail.SelectAll();
                    e.Cancel = true;
                
            } 
        }

        //Close Form
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void SupplierDetails_Load(object sender, EventArgs e)
        {

        }

     
    }
}
